package com.only4play.practice.pay.payment.kit;

import com.only4play.codegen.support.exception.BusinessException;
import com.only4play.codegen.support.model.JsonObject;
import com.only4play.practice.common.BizCodeEnum;
import com.only4play.practice.common.enums.OrderState;
import com.only4play.practice.pay.payment.core.entity.TradePaymentOrder;
import com.only4play.practice.pay.payment.kit.request.PayParamRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author mengdy
 */
public interface PaymentPlugin {

    default JsonObject<Object> unifiedOrder(HttpServletRequest request, PayParamRequest payParam) {
        throw new BusinessException(BizCodeEnum.PAY_ORDER_PAY_FAIL);
    }

    /**
     * 统一退款接口
     */
    default JsonObject<Object> unifiedRefund(HttpServletRequest request, PayParamRequest payParam){
        throw new BusinessException(BizCodeEnum.PAY_ORDER_REFUND_FAIL);
    }

    /**
     * 订单查询接口
     */
    default JsonObject<Object> orderQuery(HttpServletRequest request, PayParamRequest payParam){
        throw new BusinessException(BizCodeEnum.PAY_ORDER_NOT_EXIST);
    }

    /**
     * 统一回调
     */
    default String unifiedCallback(HttpServletRequest request,Map<String, String> params){
        throw new BusinessException(BizCodeEnum.PAY_CALLBACK_FAIL);
    }

    default String buildNotifyUrl(String domain,Integer channelType, String chanTraPayOrderNo) {
        return domain + "/pay/callback/"+channelType+"/" + chanTraPayOrderNo;
    }
    default TradePaymentOrder createPaymentOrder(PayParamRequest payParam,String chanTraPayOrderNo) {
        TradePaymentOrder item = new TradePaymentOrder();
        item.setChannelPointCode(payParam.getChannelPointCode());
        item.setStatus(OrderState.CREATED);
        item.setPayAmount(payParam.getPayAmount());
        item.setTitle(payParam.getTitle());
        item.setDescBody(payParam.getDescBody());
        item.setChanTraPayOrderNo(chanTraPayOrderNo);
//        item.setChanTraPayOrderNo("{6位年月日}{6位自增码}{2位业务码}{3位随机数}，自增码位数根据业务数据而定，用redis自增过期时间一天");
        item.setMchTradeOrderNo(payParam.getMchTradeOrderNo());
        item.setServiceName(payParam.getServiceName());
        item.setAttach(payParam.getAttach());
        item.setSpecialExt(payParam.getSpecialExt());
//        item.setClientIp(payParam.getClientIp());
        item.setOpenId(payParam.getOpenId());
        item.setAccountNo(payParam.getAccountNo());
        item.setPayTime(new Date());
        return item;
    }

    default String buildAliNotifyResponse(boolean isSuccess) {
        return isSuccess ? "success" : "fail";
    }
}
